﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Filmi.NonSemantic;
using VDS.RDF;
using System.Data;

namespace Filmi
{
    public partial class _Default : System.Web.UI.Page
    {

        PageController _pageController = new PageController();
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "") return;
            var filePath = _pageController.GetFilePathIfMovieExists(TextBox1.Text, Server.MapPath(@"~\App_Data").ToString());
            if (filePath != null)
            {
                //Imamo v bazi, v filePath je pot do datoteke, samo prebereš in naložiš
                var g = _pageController.ReadRDFFromFile(filePath);
                naloziData(_pageController.vrniData(g));                

                //TUKAJ SAMO PRIKAŽEŠ ŠE PA VOILA
                //TRIANGLES ARE MY FAVOURITE SHAPE
                PrvaSlika.ImageUrl = "Styles/img/dummies/slides/hura.jpg";
                TextBox1.Visible = false;
                Button1.Visible = false;
                PrviLabel.Visible = false;
                LabelPreTitle.Visible = true;
                LabelPreTable.Visible = true;
                ZadnjiText.Visible = true;
                PrvaTabela.Visible = true;
                DrugaTabela.Visible = true;
                TretjaTabela.Visible = true;
                CetrtaTabela.Visible = true;
            }
            else
            {
                //Nimamo v bazi, pojdi na splet po podatke
                var movieData = _pageController.GetMovieData(TextBox1.Text);
                if (movieData == null)
                {
                    //Tukaj sporoči da nismo našli nobenega zadetka
                    return;
                }

                var graph = _pageController.ReadOntologyFromFile(Server.MapPath(@"~\App_Data\3a23e007-ccba-4521-9970-985115914f8b.owl"));
                var filledGraph = _pageController.FillGraphWithData(graph, movieData);

                if (filledGraph != null)
                {
                    //TUKAJ SAMO PRIKAŽEŠ ŠE PA VOILA
                    naloziData(_pageController.vrniData(filledGraph));
                    PrvaSlika.ImageUrl = "Styles/img/dummies/slides/hura.jpg";
                    TextBox1.Visible = false;
                    Button1.Visible = false;
                    PrviLabel.Visible = false;
                    LabelPreTitle.Visible = true;
                    LabelPreTable.Visible = true;
                    ZadnjiText.Visible = true;
                    PrvaTabela.Visible = true;
                    DrugaTabela.Visible = true;
                    TretjaTabela.Visible = true;
                    CetrtaTabela.Visible = true;
                }

                else
                {
                    //Prišlo je do napake
                }
            }
        }


        private void naloziData(DataTable table)
        {

            DataRow[] oRows = table.Select("Polje like 'ImageUrl'");
            String imageurl = oRows.FirstOrDefault()[1].ToString();
            if (imageurl != "No Data")
            {
                ImageUrl.ImageUrl = imageurl;
                ImageUrl.Visible = true;
            }

            oRows = table.Select("Polje like 'TrailerURL'");
            string VideoID = oRows.FirstOrDefault()[1].ToString();
            if (VideoID != "No Data")
            {
                VideoID = VideoID.Substring(VideoID.IndexOf("v=") + 2);
                iframeDiv.Controls.Add(new LiteralControl("<iframe type=\"text/html\" width=\"640\" height=\"385\" src=\"http://www.youtube.com/embed/" + VideoID + "?autoplay=1\" frameborder=\"0\"></iframe>"));
            }
            oRows = table.Select("Polje like 'Title'");
            LabelTitle.Text = oRows.FirstOrDefault()[1].ToString();

            oRows = table.Select("Polje like 'ReleaseDate'");
            LabelReleaseDate.Text = oRows.FirstOrDefault()[1].ToString();

            oRows = table.Select("Polje like 'Rating'");
            LabelRating.Text = oRows.FirstOrDefault()[1].ToString();

            oRows = table.Select("Polje like 'Plot'");
            LabelPlot.Text = oRows.FirstOrDefault()[1].ToString();

            oRows = table.Select("Polje like 'Duration'");
            LabelDuration.Text = oRows.FirstOrDefault()[1].ToString() + " minutes";

            oRows = table.Select("Polje like 'Wins'");
            LabelWins.Text = oRows.FirstOrDefault()[1].ToString();

            oRows = table.Select("Polje like 'Nomaninations'");
            LabelNomaninations.Text = oRows.FirstOrDefault()[1].ToString();

            oRows = table.Select("Polje like 'Genre'");
            LabelGenre.Text = GetRowData(oRows);

            oRows = table.Select("Polje like 'Awards'");
            LabelAwards.Text = oRows.FirstOrDefault()[1].ToString();

            oRows = table.Select("Polje like 'Director'");

            LabelDirector.Text = GetRowData(oRows);

            oRows = table.Select("Polje like 'Star'");
            LabelStar.Text = GetRowData(oRows);

            oRows = table.Select("Polje like 'Writer'");
            LabelWriter.Text = GetRowData(oRows);

        }

        private string GetRowData(DataRow[] rows)
        {
            string link_string="";
            foreach (DataRow r in rows)
            {
                string string_data = r[1].ToString();
             if (string_data != rows.Last().ToString())
                            link_string += string_data + ", ";
                        else
                            link_string += string_data;
             }

            return link_string;
        }
    }
}
